
class Watcher {
  constructor() {
    /**
     * addWatcher(vm, watcher, key)
     * 
     * this.watchers -> watch
     * {
     *   key: ?
     *   fn: key -> fn
     * }
     */
    this.watchers = [];
  }

  addWatcher(vm, watcher, key) { // 依次添加侦听器
    this._addWatchProp({
      key,
      fn: watcher[key].bind(vm)
    })
    // console.log(this.watchers);
  }

  invoke(key, newVal, oldVal) { // 调用(watch不是更新是调用实例传进来的方法)
    this.watchers.map(item => {
      if(item.key === key)
        item.fn(newVal, oldVal);
    })
  }

  _addWatchProp(watchProp) {
    this.watchers.push(watchProp);
  }
}

export default Watcher;